﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace CollectHongKongCompanies
{
    public class InitializationDB
    {
        public void InitializationSeeder()
        {
            const string sql = "insert into Seeder (CompanyID,BuildDate) values (@companyID,getdate())";
            
            // 初始化seeder
            for (int i = 1700000; i < 1900000; i++)
            {
                using (var sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["HongKongCompanies"].ConnectionString))
                {
                    using (var sqlcmd = new SqlCommand(sql, sqlconn))
                    {
                        sqlcmd.Parameters.Add("@companyID", SqlDbType.Char, 7);
                        sqlcmd.Parameters["@companyID"].Value = i.ToString().PadLeft(7, '0');

                        try
                        {
                            sqlcmd.Connection.Open();
                            sqlcmd.ExecuteReader();
                        }
                        catch (Exception)
                        {
                            
                            throw;
                        }
                        finally
                        {
                            sqlcmd.Connection.Close();
                        }
                    }
                }
            }

            Console.WriteLine("Finished!");
        }
    }
}
